package org.railage.client.gui.tabs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.apache.log4j.Logger;
import org.railage.client.Client;
import org.railage.client.gui.main.GUI;
import org.railage.common.dto.PassengerDTO;

public class PassengersTab extends AbstractTab {

	private static final long serialVersionUID = 7869887107404755908L;

	private static final Logger log = Logger.getLogger(PassengersTab.class);

	private static final String SELECT_ROUTE = "-- Select route --";
	private static final String SELECT_TRIP = "-- Select trip ID --";

	private JComboBox<String> comboBoxRoute;
	private JComboBox<String> comboBoxTrip;
	private JTable passengersTable;

	private SimpleDateFormat birthdayFormatter = new SimpleDateFormat(
			GUI.DATE_FORMAT);

	private String[] routes = null;
	private String[] trips = null;
	private String[] columnNames = new String[] { "ID", "Surname", "Name",
			"Birthday", "Start station", "Finish station" };

	public PassengersTab() {
		setLayout(null);

		JLabel lblRoute = new JLabel("Route");
		lblRoute.setBounds(24, 22, 63, 14);
		add(lblRoute);

		comboBoxRoute = new JComboBox<String>();
		comboBoxRoute.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fillTripsComboBox((String) comboBoxRoute.getSelectedItem());
			}
		});
		comboBoxRoute.setBounds(97, 19, 200, 20);
		add(comboBoxRoute);

		JLabel lblTrip = new JLabel("Trip ID");
		lblTrip.setBounds(24, 60, 63, 14);
		add(lblTrip);

		comboBoxTrip = new JComboBox<String>();
		comboBoxTrip.setBounds(97, 57, 200, 20);
		add(comboBoxTrip);

		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fillPassengersTable((String) comboBoxTrip.getSelectedItem());
			}
		});
		btnSearch.setBounds(451, 56, 89, 23);
		add(btnSearch);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 88, 720, 302);
		add(scrollPane);

		passengersTable = new JTable();
		scrollPane.setViewportView(passengersTable);
	}

	public void init() {
		fillRoutesComboBox();
		comboBoxTrip.setModel(new DefaultComboBoxModel<String>(
				new String[] { SELECT_TRIP }));
		passengersTable.setModel(new DefaultTableModel(new Object[][] {},
				columnNames));
	}

	private void fillRoutesComboBox() {
		try {
			routes = (String[]) Client.getInstance().callMethod(
					"getRouteNames", null);
			String[] items = new String[routes.length + 1];
			items[0] = SELECT_ROUTE;
			System.arraycopy(routes, 0, items, 1, routes.length);

			comboBoxRoute.setModel(new DefaultComboBoxModel<String>(items));
		} catch (IOException e) {
			log.error(GUI.DISCON_UNEXP_LOG_MSG, e);
			JOptionPane.showMessageDialog(this, GUI.DISCON_UNEXP_GUI_MSG);
		}
	}

	private void fillTripsComboBox(String route) {
		if (!route.equals(SELECT_ROUTE)) {
			try {
				trips = (String[]) Client.getInstance().callMethod(
						"getTripIDsByRouteName", route);
				String[] items = new String[trips.length + 1];
				items[0] = SELECT_TRIP;
				System.arraycopy(trips, 0, items, 1, trips.length);

				comboBoxTrip.setModel(new DefaultComboBoxModel<String>(items));
			} catch (IOException e) {
				log.error(GUI.DISCON_UNEXP_LOG_MSG, e);
				JOptionPane.showMessageDialog(this, GUI.DISCON_UNEXP_GUI_MSG);
			}
		} else {
			comboBoxTrip.setModel(new DefaultComboBoxModel<String>(
					new String[] { SELECT_TRIP }));
		}
	}

	private void fillPassengersTable(String trip) {
		Object[][] data = new Object[][] {};
		if (!trip.equals(SELECT_TRIP)) {
			PassengerDTO[] passengers = null;
			try {
				passengers = (PassengerDTO[]) Client.getInstance().callMethod(
						"getPassengersByTrip", Integer.parseInt(trip));
				data = new Object[passengers.length][];
				int i = 0;
				for (PassengerDTO passenger : passengers) {
					data[i] = new Object[] { passenger.getId(),
							passenger.getSurname(), passenger.getName(),
							birthdayFormatter.format(passenger.getBirthday()),
							passenger.getStartStation(),
							passenger.getFinishStation() };
					i++;
				}
			} catch (IOException e) {
				log.error(GUI.DISCON_UNEXP_LOG_MSG, e);
				JOptionPane.showMessageDialog(this, GUI.DISCON_UNEXP_GUI_MSG);
			}
		}
		passengersTable.setModel(new DefaultTableModel(data, columnNames) {
			private static final long serialVersionUID = 4735904666373382366L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		});
	}
}
